package com.cq.smartelebicycle.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cq.smartelebicycle.R;

/**
 * @author leon
 */
public class FitnessFragment extends Fragment implements OnClickListener{
	
	public static final String TAG_ID = "FitnessFragment";
    private TextView tvPercent;
	private ImageView ivReduce;
	private ImageView ivProgress;
	private ImageView ivPlus;
	private TextView tvSpeed;
	private TextView tvPowerGenerated;
	private TextView tvCalorieConsume;
	private TextView tvMileageThisTime;
	private ImageButton ibMileageEmpty;
	private TextView tvTotalMileage;
	private int fitnessPercent=0;
	private int fitnessMileageThisTime;
	//get the Display Metrics
	private int displayWidth;

	private enum Operateion {REDUCE,PLUS}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fitness_fragment, container, false);
		initView(rootView);
		return rootView;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		displayWidth = mDisplayMetrics.widthPixels;
		Drawable drawable = getActivity().getResources().getDrawable(R.drawable.jindutiao);
		int mWidth = drawable.getIntrinsicWidth();
		int mHeight = drawable.getIntrinsicHeight();
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(displayWidth/2, displayWidth/2*mHeight/mWidth+1);
		ivProgress.setLayoutParams(layoutParams);
	}
	/**
	 * init the view
	 * @param view
	 */
	private void initView(View root){
		tvPercent = (TextView) root.findViewById(R.id.fitness_adjust_percent);
		ivReduce = (ImageView) root.findViewById(R.id.fitness_adjust_reduce);
		ivReduce.setOnClickListener(this);
		ivProgress = (ImageView) root.findViewById(R.id.fitness_adjust_progress);
		ivProgress.setOnClickListener(this);
		ivPlus = (ImageView) root.findViewById(R.id.fitness_adjust_plus);
		ivPlus.setOnClickListener(this);
		tvSpeed = (TextView) root.findViewById(R.id.fitness_speed);
		tvPowerGenerated = (TextView) root.findViewById(R.id.fitness_power_generated);
		tvCalorieConsume = (TextView) root.findViewById(R.id.fitness_calorie_consume);
		tvMileageThisTime = (TextView) root.findViewById(R.id.fitness_mileage_this_time);
		ibMileageEmpty = (ImageButton) root.findViewById(R.id.fitness_mileage_empty);
		ibMileageEmpty.setOnClickListener(this);
		tvTotalMileage = (TextView) root.findViewById(R.id.fitness_total_mileage);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fitness_adjust_reduce:
			fitnessAdjust(Operateion.REDUCE);
			break;
		case R.id.fitness_adjust_plus:
			fitnessAdjust(Operateion.PLUS);
			break;
		case R.id.fitness_mileage_empty:
			fitnessEmpty();
			break;
		default:
			break;
		}
	}
	
	private void fitnessAdjust(Operateion operateion){
		if(operateion==Operateion.REDUCE){
			fitnessPercent=fitnessPercent<10?0:fitnessPercent-10;
			tvPercent.setText(fitnessPercent==0?"0":fitnessPercent+"%");
		}else{
			fitnessPercent=fitnessPercent>=100?100:fitnessPercent+10;
			tvPercent.setText(fitnessPercent+"%");
		}
	}
	
	private void fitnessEmpty(){
		tvMileageThisTime.setText("0000");
		fitnessMileageThisTime = 0;
	}
}
